package com.beiben.work.process.controller;

import com.beiben.common.GlobalConstant;
import com.beiben.system.file.domain.File;
import com.beiben.system.file.service.IFileService;
import com.beiben.system.user.domain.BeiBenUser;
import com.beiben.system.user.service.IBeiBenUserService;
import com.beiben.work.process.domain.ExportProcessVo;
import com.beiben.work.process.domain.FileProcess;
import com.beiben.work.process.domain.FileProcessFinishedVm;
import com.beiben.work.process.service.IFileProcessService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.util.List;

/**
 * 文件任务管理Controller
 *
 * @author xgq
 * @date 2020-08-26
 */
@RestController
@RequestMapping("/work/process")
public class FileProcessController extends BaseController {

    @Autowired
    private IFileProcessService fileProcessService;

    @Autowired
    private IFileService fileService;

    /**
     * 查询文件任务管理列表
     */
    @PreAuthorize("@ss.hasPermi('work:process:list')")
    @GetMapping("/list")
    public TableDataInfo list(FileProcess fileProcess) throws ParseException {
        startPage();
        List<FileProcess> list = fileProcessService.selectFileProcessList(fileProcess);
        return getDataTable(list);
    }

    /**
     * 导出文件任务管理列表
     */
    @PreAuthorize("@ss.hasPermi('work:process:export')")
    @Log(title = "文件任务管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FileProcess fileProcess) throws ParseException {
        List<FileProcess> list = fileProcessService.selectFileProcessList(fileProcess);
        ExcelUtil<FileProcess> util = new ExcelUtil<FileProcess>(FileProcess.class);
        return util.exportExcel(list, "process");
    }

    /**
     * 获取文件任务管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('work:process:query')")
    @GetMapping(value = "/{workId}")
    public AjaxResult getInfo(@PathVariable("workId") Long workId) {
        return AjaxResult.success(fileProcessService.selectFileProcessById(workId));
    }

    /**
     * 新增文件任务管理
     */
    @PreAuthorize("@ss.hasPermi('work:process:add')")
    @Log(title = "文件任务管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FileProcess fileProcess) {
        return toAjax(fileProcessService.insertFileProcess(fileProcess));
    }

    /**
     * 修改文件任务管理
     */
    @PreAuthorize("@ss.hasPermi('work:process:edit')")
    @Log(title = "文件任务管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FileProcess fileProcess) {
        return toAjax(fileProcessService.updateFileProcess(fileProcess));
    }

    /**
     * 删除文件任务管理
     */
    @PreAuthorize("@ss.hasPermi('work:process:remove')")
    @Log(title = "文件任务管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{workIds}")
    public AjaxResult remove(@PathVariable Long[] workIds) {
        return toAjax(fileProcessService.deleteFileProcessByIds(workIds));
    }

    /**
     * 导出文件任务管理列表
     */
    @GetMapping("/exportProcessList")
    public TableDataInfo exportProcessList(ExportProcessVo exportProcessVo) throws ParseException {
        startPage();
        List<ExportProcessVo> list = fileProcessService.handleExport(exportProcessVo);
        return getDataTable(list);
    }
    /**
     * 导出文件任务管理列表
     */
    @Log(title = "导出任务明细", businessType = BusinessType.EXPORT)
    @GetMapping("/handleExport")
    public AjaxResult handleExport(ExportProcessVo exportProcessVo) throws ParseException {
        List<ExportProcessVo> list = fileProcessService.handleExport(exportProcessVo);
        ExcelUtil<ExportProcessVo> util = new ExcelUtil<ExportProcessVo>(ExportProcessVo.class);
        return util.exportExcel(list, "process");
    }

    /**
     * 上传见证性材料
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile[] file, Long tableId) {
        try {
            fileService.uploadFile(file, tableId, GlobalConstant.WORK_FILE_PROCESS,GlobalConstant.WORK_FILE_SERVER_ADDRESS);
            AjaxResult ajax = AjaxResult.success();
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
     * 按itemId获取文件列表
     *
     * @return
     */
    @PostMapping("/files/{workId}")
    public TableDataInfo selectFiles(@PathVariable Long workId) {
        startPage();
        List<File> list = fileProcessService.selectFiles(workId);
        return getDataTable(list);
    }

    /**
     * 根据部门编号获取部门人员详细信息
     */
    @GetMapping(value = "/user")
    public AjaxResult getUserInfo(@RequestParam("workId") Long workId)
    {
        List<BeiBenUser> data = fileProcessService.getUserInfo(workId);
        return AjaxResult.success(data);
    }

    /**
     * 根据登录用户查询该用的角色及已提交的工单情况，做提醒
     */
    @GetMapping(value = "/remindMe")
    public AjaxResult remindMe(){
       return fileProcessService.remindMe();
    }
}
